<?php

namespace app\home\controller\user;

use app\home\controller\AuthController;
use services\JsonService;
use services\UtilService;
use traits\CurdControllerTrait;
use app\home\model\system\SystemUserLevel;
use services\FormBuilder as Form;
use think\facade\Route as Url;

/**
 * 会员等级
 * Class UserLevel
 * @package app\home\controller\user
 */
class UserLevel extends AuthController
{
    use CurdControllerTrait;

    /**
     * 首页的数据
     * @return string
     */
    public function index()
    {
        return $this->fetch();
    }


    /**
     * 获取所有的会员的等级
     * @return false|string
     */
    public function get_system_vip_list()
    {
        $where = UtilService::getMore([
            ["page", 0],
            ["limit", 10],
            ["title", ''],
            ["is_show", ''],
        ]);
        return JsonService::successlayui(SystemUserLevel::getSytemList($where));
    }

    /**
     * 新增等级
     * @param int $id
     * @return string
     * @throws \FormBuilder\exception\FormBuilderException
     */
    public function create($id = 0)
    {
        if ($id) $vipinfo = SystemUserLevel::get($id);
        $field[] = Form::input('name', '等级名称', isset($vipinfo) ? $vipinfo->name : '')->col(Form::col(24));
        $field[] = Form::radio('is_forever', '是否为永久', isset($vipinfo) ? $vipinfo->is_forever : 0)->options([['label' => '永久', 'value' => 1], ['label' => '非永久', 'value' => 0]])->col(24);
        //$field[]= Form::number('money','等级价格',isset($vipinfo) ? $vipinfo->money : 0)->min(0)->col(24);
        //$field[]= Form::radio('is_pay','是否需要购买',isset($vipinfo) ? $vipinfo->is_pay : 0)->options([['label'=>'需要','value'=>1],['label'=>'免费','value'=>0]])->col(24);
        $field[] = Form::number('valid_date', '有效时间(天)', isset($vipinfo) ? $vipinfo->valid_date : 0)->min(0)->col(8);
        $field[] = Form::number('grade', '等级', isset($vipinfo) ? $vipinfo->grade : 0)->min(0)->col(8);
        $field[] = Form::number('discount', '享受折扣', isset($vipinfo) ? $vipinfo->discount : 0)->min(0)->col(8);
        $field[] = Form::frameImageOne('icon', '图标', Url::buildUrl('home/widget.images/index', array('fodder' => 'icon')), isset($vipinfo) ? $vipinfo->icon : '')->icon('image')->width('100%')->height('500px');
        $field[] = Form::frameImageOne('image', '会员背景', Url::buildUrl('home/widget.images/index', array('fodder' => 'image')), isset($vipinfo) ? $vipinfo->image : '')->icon('image')->width('100%')->height('500px');
        $field[] = Form::radio('is_show', '是否显示', isset($vipinfo) ? $vipinfo->is_show : 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(8);
        $field[] = Form::textarea('explain', '等级说明', isset($vipinfo) ? $vipinfo->explain : '');
        $form = Form::make_post_form('添加等级设置', $field, Url::buildUrl('save', ['id' => $id]), 2);
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }

    /**
     * 保存等级
     */
    public function save($id = 0)
    {
        $data = UtilService::getMore([
            ["name", ""],
            ["is_forever", 0],
            ["money", 0],
            ["is_pay", 0],
            ["valid_date", 0],
            ["grade", 0],
            ["discount", 0],
            ["icon", ""],
            ["image", ""],
            ["is_show", ""],
            ["explain", ""]
        ]);
        if (!$data['name']) return JsonService::fail('请输入等级名称');
        if (!$data['grade']) return JsonService::fail('请输入等级');
        if (!$data['explain']) return JsonService::fail('请输入等级说明');
        if ($data['is_forever'] == 0 && !$data['valid_date']) return JsonService::fail('请输入有效时间(天)');
        if ($data['is_pay']) return JsonService::fail('会员等级购买功能正在开发中，暂时关闭可购买功能！');
        if ($data['is_pay'] && !$data['money']) return JsonService::fail('请输入购买金额');
        if (!$data['icon']) return JsonService::fail('请上传等级图标');
        if (!$data['image']) return JsonService::fail('请上传等级背景图标');
        if (!$id && SystemUserLevel::be(['is_del' => 0, 'grade' => $data['grade']])) return JsonService::fail('已检测到您设置过的会员等级，此等级不可重复');
        SystemUserLevel::beginTrans();
        try {
            //修改
            if ($id) {
                if (SystemUserLevel::edit($data, $id)) {
                    SystemUserLevel::commitTrans();
                    return JsonService::successful('修改成功');
                } else {
                    SystemUserLevel::rollbackTrans();
                    return JsonService::fail('修改失败');
                }
            } else {
                //新增
                $data['add_time'] = time();
                $data["wxapp_id"] = $this->wxapp_id;
                if (SystemUserLevel::create($data)) {
                    SystemUserLevel::commitTrans();
                    return JsonService::successful('添加成功');
                } else {
                    SystemUserLevel::rollbackTrans();
                    return JsonService::fail('添加失败');
                }
            }
        } catch (\Exception $exception) {
            SystemUserLevel::rollbackTrans();
            return JsonService::fail($exception->getMessage());
        }
    }
}